﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
       
                //随机数
        private static int iSeed = 8;
        Random rd = new Random(iSeed);
        //存放数据的数组最大值
        private int sizeMax;
        //存放y轴数据的数组链表
        private List<int> DataL;
        private List<int> DataL1;
        private List<int> DataL2;
        //存放在画布上的数据节点的数组
        private Point[] pArrData;
        private Point[] pArrData2;
        private Point[] pArrData3;
        private string mystr = "";

        public Form1()
        {
            //初始化
            InitializeComponent();
            //根据画布的宽决定x轴需要多少个数组
            sizeMax = pcbDisplay.Width / 10;
            //数据数组
            DataL = new List<int>();
            DataL1 = new List<int>();
            DataL2 = new List<int>();
            pArrData = new Point[sizeMax + 1];
            pArrData2 = new Point[sizeMax + 1];
            pArrData3 = new Point[sizeMax + 1];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = sizeMax.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DataL.Add(rd.Next(0,329));
            DataL1.Add(rd.Next(50,200));
            DataL2.Add(rd.Next(50, 200));
            //数据链表是否达到x轴最大容量的数组（动态曲线的来源）
            if (DataL.Count == sizeMax + 2)
            {
                DataL.RemoveAt(0);//移除链表第一个
            }

            if (DataL1.Count == sizeMax +2)
            {
                DataL1.RemoveAt(0);
            }
            if (DataL2.Count == sizeMax + 2)
            {
                DataL2.RemoveAt(0);
            }
            //string pc = "本软件由韩东宇提供技术支持 | 2017-04";
            //toolStripStatusLabel1.Text = pc +" | "+ DataL.Count.ToString(); 
            //判断数据链表是否为空
            if (DataL.Count != 0)
            {
                pArrData = new Point[DataL.Count];
            }
            if (DataL1.Count != 0)
            {
                pArrData2 = new Point[DataL1.Count];
            }
            if (DataL2.Count != 0)
            {
                pArrData3 = new Point[DataL2.Count];
            }

            //生成新的节点
            for (int i = 0; i < sizeMax+ 1; i++)
            {
                if (i >= DataL.Count)
                {
                    break;
                }
                pArrData[i] = new Point(i * 10, DataL[i]);
                pArrData2[i] = new Point(i*10,DataL1[i]);
                pArrData3[i] = new Point(i*10,DataL2[i]);
            }
            foreach (Point v in pArrData)
            {
                mystr += v.ToString();
            }
            //toolStripStatusLabel1.Text = mystr;
            pcbDisplay.Refresh();
        }

        #region 绘制曲线

        //定义画笔
        private Pen greenPen = new Pen(Color.Green, 3);
        private Pen redPen = new Pen(Color.Red, 2);
        private Pen blackPen = new Pen(Color.Black, 2);

        /// <summary>
        /// 绘制曲线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void pcbDisplay_Paint(object sender, PaintEventArgs e)
        {
            if (DataL.Count != 1)
            {
                e.Graphics.DrawCurve(greenPen, pArrData);
                //e.Graphics.DrawCurve(redPen, pArrData2);
                //e.Graphics.DrawCurve(blackPen, pArrData3);

            }
        }







        #endregion

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                timer1.Start();
                button1.Text = "暂停";
                button1.ForeColor = Color.Red;
            }else
            {
                timer1.Stop();
                button1.Text = "测压开始";
            }
        }
    }
}
